<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Orderexcel_m extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    //交运对账信息
    public function getDeliverBill($objExcel,$deliverType){
        //E邮宝或cnpost 或燕文
        if($deliverType==8||$deliverType==11||$deliverType==7){
            $sheet = $objExcel->getSheet(0);
        }
        //云途
        if($deliverType==6){
            $sheet = $objExcel->getSheet(4);
        }
        $rows = $sheet->getHighestRow(); //总行数
        $cols = $sheet->getHighestColumn(); //总列数 英文大写字母
        $arrCellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU',
            'AV','AW','AX','AY','AZ');
        $cols_length = array_search($cols, $arrCellName); //获取数组下标最大数
        $arr_deliver = array();
        $str_conn = "";
        /**
         * 燕文 begin
         */
        if($deliverType==7){
            //行
            for($row=1;$row<=$rows;$row++){
                //列
                for($column=0;$column<= $cols_length;$column++){
                    $cellValue = $arrCellName[$column].$row; //A2
                    $col_val = $sheet->getCell($cellValue)->getValue();
                    $col_val_len = strlen($col_val);
                    //运单号
                    if($column==1){
                        //燕文
                        if($col_val_len == 11 || $col_val_len==13){
                            $arr_deliver[$row]['deliverNumber'] = $col_val;
                            $str_conn.="'".$col_val."'".",";
                        }else{
                            break;
                        }
                    }
                    if($column==6){
                        $arr_deliver[$row]['deliverWeight'] = $col_val;
                    }
                    //资费
                    if($column==13){
                        $arr_deliver[$row]['deliverFee'] = $col_val;
                    }

                }
            }
        }
        //燕文 end
        /**
         * 云途 begin
         */
        if($deliverType==6){
            //行
            for($row=1;$row<=$rows;$row++){
                //列
                for($column=0;$column<= $cols_length;$column++){
                    $cellValue = $arrCellName[$column].$row; //A2
                    $col_val = $sheet->getCell($cellValue)->getValue();
                    $col_val_len = strlen($col_val);
                    //运单号
                    if($column==0){
                        //燕文
                        if($col_val_len == 18){
                            $arr_deliver[$row]['deliverNumber'] = $col_val;
                            $str_conn.="'".$col_val."'".",";
                        }else{
                            break;
                        }
                    }
                    if($column==11){
                        $arr_deliver[$row]['deliverWeight'] = $col_val;
                    }
                    //资费
                    if($column==19){
                        $arr_deliver[$row]['deliverFee'] = $col_val;
                    }

                }
            }
        }
        //云途 end
        /**
         * E邮宝 cnpost begin
         */
        if($deliverType==8||$deliverType==11){
            //行
            for($row=1;$row<=$rows;$row++){
                //列
                for($column=0;$column<= $cols_length;$column++){
                    $cellValue = $arrCellName[$column].$row; //A2
                    $col_val = $sheet->getCell($cellValue)->getValue();
                    $col_val_len = strlen($col_val);
                    //运单号
                    if($column==0){
                        //燕文
                        if($col_val_len == 13){
                            $arr_deliver[$row]['deliverNumber'] = $col_val;
                            $str_conn.="'".$col_val."'".",";
                        }else{
                            break;
                        }
                    }
                    if($column==3){
                        $arr_deliver[$row]['deliverWeight'] = $col_val;
                    }
                    //资费
                    if($column==7){
                        $arr_deliver[$row]['deliverFee'] = $col_val;
                    }

                }
            }
        }
        //E邮宝 cnpost end

        $arr_tmp = array();
        $deliver_sort = array();
        $total_money = 0;
        $fuman_money = 0;
        $chajia_money = 0;
        $str_conn = trim($str_conn,",");
        $sql = "SELECT order_id,shipping_number,orderweight,shipping_fee,order_sn,actionorderweight,shipping_expectfee FROM db_order_list WHERE shipping_number in ($str_conn) ";
        $rs  = $this->db->query($sql)->result_array();
        //print_r($sql);exit;
        foreach($rs as $key=>$value){
            $arr_tmp[] = $value['shipping_number'];
        }
        foreach($arr_deliver as $kk=>$vv){
            if(in_array($vv['deliverNumber'],$arr_tmp)){
                //获取键值
                $key = array_search($vv['deliverNumber'],$arr_tmp);
                $weight = $rs[$key]['actionorderweight'];
                $fee = $rs[$key]['shipping_expectfee'];
                $order_sn = $rs[$key]['order_sn'];

                $arr_deliver[$kk]['id']=$kk;
                $arr_deliver[$kk]['deliverNumber']=$vv['deliverNumber'];
                $arr_deliver[$kk]['deliverFee']=$vv['deliverFee'];
                $arr_deliver[$kk]['deliverWeight']=$vv['deliverWeight'];
                //复漫
                $arr_deliver[$kk]['orderId']= $order_sn;
                $arr_deliver[$kk]['fee']= $fee;
                $arr_deliver[$kk]['weight']= $weight;
                $total_money = $total_money+$vv['deliverFee'];
                $fuman_money = $fuman_money+$fee;
                $chajia_money= $chajia_money+$vv['deliverFee'];
                //sort
                $deliver_sort[$kk] = $order_sn;

                    //更新数据库运费相关信息 8/29 //判断是否更新过，若更新过，则不用操作
                $sql_orderlist = " SELECT * FROM db_order_list WHERE order_id = ".$rs[$key]['order_id'];
                $rs_orderlist  = $this->db->query($sql_orderlist)->row_array();
                if($rs_orderlist['shipping_realfee']==$vv['deliverFee']){
                }else{
                    $arr_plat['shipping_realfee']=$vv['deliverFee'];
                    if($rs[$key]['actionorderweight']){
                        $arr_plat['orderweight']=$rs[$key]['actionorderweight'];
                    }
                    $this->db->update('db_order_list',$arr_plat,"order_id=".$rs[$key]['order_id']); //更新物流费用
                    //更新日志表db_goods_log
                    $imgdata["goods_id"] = $rs[$key]['order_id'];	//订单ID db_order_list
                    $imgdata["updatetime"] = date("Y-m-d H:i:s");
                    $imgdata["createUid"] = $_COOKIE['userid'];
                    $imgdata["types"] = 3;
                    $imgdata["content"] = '交运对帐处理，更新真实物流费为：'.$vv['deliverFee'].',更新预估重量为：'.$rs[$key]['actionorderweight'];
                    $imgdata["createUser"] =$_COOKIE['username'];
                    $this->db->insert('db_goods_log', $imgdata);
                }

            }else{
                $arr_deliver[$kk]['id']=$kk;
                $arr_deliver[$kk]['deliverNumber']=$vv['deliverNumber'];
                $arr_deliver[$kk]['deliverFee']=$vv['deliverFee'];
                $arr_deliver[$kk]['deliverWeight']=$vv['deliverWeight'];
                $arr_deliver[$kk]['orderId']= "";
                $arr_deliver[$kk]['fee']= 0;
                $arr_deliver[$kk]['weight']= 0;
                $total_money = $total_money+$vv['deliverFee'];
                $fuman_money = $fuman_money+0;
                $chajia_money= $chajia_money+0;

                $deliver_sort[$kk] = "";
            }
        }
        @array_multisort($deliver_sort,SORT_ASC,$arr_deliver); //排序

        //print_r($sql);exit;
        $arr_rs = array($arr_deliver,$total_money,$fuman_money,$chajia_money);
        return $arr_rs;
    }



}

/* End of file Orderlist_m.php */